#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 2005;
const ll INF = 1e16;
int n, mxp, w;
int ap[N], bp[N], as[N], bs[N];
ll mx[N];
ll f[N][N];
ll g(int v, int p) { return mx[v] + 1ll * v * p; }

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> mxp >> w;
  rep(i, 1, n) cin >> ap[i] >> bp[i] >> as[i] >> bs[i];
  rep(i, 1, mxp) f[0][i] = mx[i] = -INF;
  rep(i, 1, n) {
    rep(j, 0, mxp) f[i][j] = f[i - 1][j];  // no action
    int pre = max(0, i - w - 1);
    if (pre > 0) rep(j, 0, mxp) mx[j] = max(mx[j], f[pre][j]);
    deque<int> dq;
    // buy
    int k = 0;
    rep(j, 1, mxp) {
      int l = max(0, j - as[i]);
      while (k < j) {
        while (dq.size() && g(dq.back(), ap[i]) <= g(k, ap[i])) dq.pop_back();
        dq.push_back(k++);
      }
      while (dq.size() && dq.front() < l) dq.pop_front();
      ll now = g(dq.front(), ap[i]) - 1ll * j * ap[i];
      f[i][j] = max(f[i][j], now);
    }
    dq.clear();
    // sold
    k = mxp;
    per(j, mxp - 1, 0) {
      int r = min(mxp, j + bs[i]);
      while (k > j) {
        while (dq.size() && g(dq.back(), bp[i]) <= g(k, bp[i])) dq.pop_back();
        dq.push_back(k--);
      }
      while (dq.size() && dq.front() > r) dq.pop_front();
      ll now = g(dq.front(), bp[i]) - 1ll * j * bp[i];
      f[i][j] = max(f[i][j], now);
    }
    // rep(j, 0, mxp) cout << i << ' ' << j << ' ' << f[i][j] << endl;
  }
  ll ans = -INF;
  rep(i, 0, mxp) ans = max(f[n][i], ans);
  cout << ans;
  return 0;
}